<?php

namespace Home\Controller;


/**
 * 收款码控制器
 *
 */
class ShouKuanController extends HomeController
{


    /**
     * 添加收款码
     */
    public function add(){
        $uid = I('get.uid');
        $params = I('post.');
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        if($params['hb'] < $gather['up_set']){
            $this->showError('花呗费率不能低于自己的');
        }
        if($params['hb'] > 20){
            $this->showError('花呗费率不能大于20');
        }

        if($params['xyk'] > 10){
            $this->showError('pos费率不能大于10');
        }
        if($params['xyk'] < $gather['up_set2']){
            $this->showError('pos费率不能低于自己的');
        }


        $params['uid'] = $uid;
        $params['createtime'] = time();
        $res = M('shoukuanma')->add($params);
        if($res){
            $this->showSuccess('添加成功');
        }else{
            $this->showError('添加失败');
        }
    }

    /**
     * 修改收款码信息
     */
    public function edit(){
        $id = I('get.id');
        $params = I('post.');

        $res = M('shoukuanma')->where('id='.$id)->save($params);
        if($res !== false){
            $this->showSuccess('修改成功');
        }else{
            $this->showError('修改失败');
        }
    }

    /**
     * 删除收款码
     */
    public function del(){
        $id = I('get.id');

        $res = M('shoukuanma')->where('id='.$id)->delete();
        if($res !== false){
            $this->showSuccess('删除成功');
        }else{
            $this->showError('删除失败');
        }
    }

    /**
     * 收款码信息列表
     */
    public function index(){
        $list = M('shoukuanma')->where('uid='.$this->uid)->select();

        $this->showSuccess('',$list);
    }

    /**
     * 收款二维码
     */
    public function qrcode(){
        $id = I('get.id');
        $one = D('Ptyh')->where('uid=' . $this->uid)->find();
        $gather = D('Gather')->where('uid=' . $this->uid)->find();
        if ($one['identify'] == 0) {

            $this->showError('请先绑定您的信息');

        }

        if (empty($one['status'])) {

            $this->showError('您的认证信息有待审核，请耐心等候！');

        }

        $type = I('get.type');
        if (empty($type)) {
            $type = 1;

        }
        $uid = $this->uid;
        if ($type == 1) {

            //信用卡支付
            $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment2&uid={$uid}&id={$id}";
        } else if ($type == 2) {
            if ($gather['status_fq'] == 1 || $gather['status_hb'] == 1 || $gather['status_xy'] == 1) {

                $url = "http://qnm.upslope.cn/index.php?m=weixin&c=user&a=yh_payment3&uid={$uid}&id={$id}";
            } else {

                $this->showError('您没有权限使用普通码！');
            }
        }

        $url = urlencode($url);

        $shoukuanma = M('shoukuanma')->where('id='.$id)->find();
        $data = [
            'id' => $shoukuanma['id'],
            'type' => $type,
            'name' => $shoukuanma['name'],
            'url' => $url
        ];
        $this->showSuccess('',$data);
    }

}